import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { categoryDeleteById } from '@/api/category.ts'
import { ElMessage, ElMessageBox } from 'element-plus'
import { categoryList, categoryPage } from '@/api/users.ts'

export const useDilogoStore = defineStore('dilogo', () => {
  const dialogFormVisible = ref(false)
  const tableData = ref([])
  const currentPage4 = ref(4)
  const pageSize = ref(3)
  const background = ref(false)
  const disabled = ref(false)
  const pageNum = ref(1)
  const categoryName = ref('')
  const total = ref(0)
  const records = ref([])
  const page = ()=>{
    const params = {
      categoryName: categoryName.value,
      pageNum: pageNum.value,
      pageSize: pageSize.value
    }
    categoryPage(params).then((res) => {
      if (res.data.code == 200) {
        console.log(res.data)
        total.value = res.data.data.total
        records.value = res.data.data.list
        console.log("111"+tableData.value)
      } else {
        ElMessage.error("渲染失败")
      }
    })
  }

  const handleSizeChange = (pageSizex: number) => {
    pageSize.value = pageSizex
    console.log("size"+pageSize.value)
    // useDilogoStorex.List()
  }

  const handleCurrentChange = (pages: number) => {
    pageNum.value = pages
    console.log(pageNum.value)
    page()
    // useDilogoStorex.List()
  }

  const List = () => categoryList().then(res=>{
    if (res.data.code == 200){
      tableData.value = res.data.data
      // console.log(form.value.tableData)
    }else {
      ElMessage.error("渲染失败")
    }
  })




  const handleDelete = async (id: string) => {
   await ElMessageBox.confirm('确定删除吗？', '删除提示', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
   }).catch(() => {
     ElMessage.info('已取消删除')
     return new Promise(() => {}) // 返回一个空Promise，防止后续代码执行
   })
   const res = await categoryDeleteById(id)
    if (res.data.code == 200){
      ElMessage.success("删除成功")
      List()
    }else {
      ElMessage.error("删除失败")
    }
  }
  return { dialogFormVisible,useDilogoStore,tableData,handleDelete,List,categoryPage,page,handleSizeChange,handleCurrentChange,pageSize,pageNum,background,disabled,categoryName,total,records,currentPage4 }
})
